package com.wuye.charge.service.impl;


import com.wuye.charge.domain.WyCost;
import com.wuye.charge.domain.WyCostAllocation;
import com.wuye.charge.domain.WyCostMake;
import com.wuye.charge.domain.WyTb;
import com.wuye.charge.domain.vo.*;
import com.wuye.charge.mapper.*;
import com.wuye.charge.service.IWyCostMakeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 费用生成Service业务层处理
 *
 * @author lishuo
 * @date 2024-11-22
 */
@Service
public class WyCostMakeServiceImpl implements IWyCostMakeService {
    @Autowired
    private WyCostMakeMapper wyCostMakeMapper;
    @Autowired
    private WyCostMapper wyCostMapper;
    @Autowired
    private WyCostAllocationMapper wyCostAllocationMapper;
    @Autowired
    private WyBalconyunithomeVoMapper wyBalconyunithomeVoMapper;
    @Autowired
    private WyTbMapper wyTbMapper;

    /**
     * 查询费用生成
     *
     * @param makeId 费用生成主键
     * @return 费用生成
     */
    @Override
    public WyCostMake selectWyCostMakeByMakeId(Long makeId) {
        return wyCostMakeMapper.selectWyCostMakeByMakeId(makeId);
    }

    /**
     * 查询费用生成列表
     *
     * @param wyCostMake 费用生成
     * @return 费用生成
     */
    @Override
    public List<WyCostMake> selectWyCostMakeList(WyCostMake wyCostMake) {
        List<WyCostMake> wyCostMakes = wyCostMakeMapper.selectWyCostMakeList(wyCostMake);
        if (!wyCostMakes.isEmpty()){
            for (WyCostMake item : wyCostMakes){
                WyBalconyunithomeVo wyBalconyunithomeVo = new WyBalconyunithomeVo();
                wyBalconyunithomeVo.setId(item.getBalconyunithomeId());
                List<WyBalconyunithomeVo> wyBalconyunithomeVos = wyBalconyunithomeVoMapper.selectWyBalconyunithomeVoList(wyBalconyunithomeVo);
                if (!wyBalconyunithomeVos.isEmpty()){
                    item.setWyBalconyunithomeVo(wyBalconyunithomeVos.get(0));
                }
                WyCommunityVo wyCommunityVo = wyBalconyunithomeVoMapper.selectWyCommunityVoById(item.getBalconyunithomeId());
                item.setWyCommunityVo(wyCommunityVo);
            }
        }
        return wyCostMakes;
    }

    /**
     * 新增费用生成
     *
     * @param wyCostMake 费用生成
     * @return 结果
     */
//    @Override
//    public int insertWyCostMake(WyCostMake wyCostMake)
//    {
//        return wyCostMakeMapper.insertWyCostMake(wyCostMake);
//    }
    @Override
    public int insertWyCostMake(WyCostMake wyCostMake) {
        int result = 0;
        List<Long> ids = wyCostMake.getBalconyunithomeIds();
        if (ids == null || ids.isEmpty()) {
            return 0;
        }
        Date startTime = wyCostMake.getMakeStartTime();/*合并起始时间*/
        Date endTime = wyCostMake.getMakeEndTime();/*合并截至时间*/
        long daysBetween = getDaysBetween(startTime, endTime);
        WyCostAllocation wyCostAllocation = new WyCostAllocation();
        wyCostAllocation.setBalconyunithomeIds(ids);
        wyCostAllocation.setAlocationStatus("0");
        List<WyCostAllocation> wyCostAllocations = wyCostAllocationMapper.selectWyCostAllocationList(wyCostAllocation);
        Map hashMap = new HashMap();
        if(!wyCostAllocations.isEmpty()){
            for (WyCostAllocation item : wyCostAllocations){
                hashMap.put(item.getCostId(), item);
            }
            wyCostAllocations.clear();
            for(Object value: hashMap.values()){
                wyCostAllocations.add((WyCostAllocation) value);
            }
        }
        if (!wyCostAllocations.isEmpty()) {
            for (WyCostAllocation item : wyCostAllocations) {
                WyCost wyCost = wyCostMapper.selectWyCostByCostId(item.getCostId());
                WyCostMake costMake = new WyCostMake();
                costMake.setMakeStartTime(startTime);/*起始时间*/
                costMake.setMakeEndTime(endTime);/*截至时间*/
                costMake.setBalconyunithomeId(item.getBalconyunithomeId());/*中间表id*/
                costMake.setMakeContent(wyCost.getCostName());/*费用内容*/
                costMake.setMakeCreateTime(new Date());/*生成时间*/
                /*小区*/
                costMake.setCommunicateId(wyBalconyunithomeVoMapper.selectWyCommunityVoById(item.getBalconyunithomeId()).getCommunityId());
                Long methodId = wyCost.getMethodId();
                WyCost cost = wyCostMapper.selectWyCostByCostId(item.getCostId());
                costMake.setCostLate(cost.getCostLate());
                if (methodId == 1) {/* 建筑面积*单价/天 */
                    WyHomeVo wyHomeVo = wyBalconyunithomeVoMapper.selectWyHomeVoById(item.getBalconyunithomeId());
                    if(wyHomeVo.getArea()!=null){
                        costMake.setMakeTotal((new BigDecimal(wyHomeVo.getArea().toString()).multiply(wyCost.getCostPrice()).multiply(BigDecimal.valueOf(daysBetween))).add(wyCost.getCostMore()));
                        int i = wyCostMakeMapper.insertWyCostMake(costMake);
                        result += i;
                        WyTb wyTb = wyTbMapper.selectById(2);
                        if(wyTb!=null&&costMake.getMakeTotal()!=null){
                            wyTb.setTbNeedMoney(wyTb.getTbNeedMoney().add(costMake.getMakeTotal()));
                            wyTbMapper.updateById(wyTb);
                        }
                    }


                } else if (methodId == 0) {/* 建筑面积*单价/月 */
                    WyHomeVo wyHomeVo = wyBalconyunithomeVoMapper.selectWyHomeVoById(item.getBalconyunithomeId());
                    if(wyHomeVo.getArea()!=null){
                        costMake.setMakeTotal((new BigDecimal(wyHomeVo.getArea().toString()).multiply(wyCost.getCostPrice()).multiply(BigDecimal.valueOf(30))).add(wyCost.getCostMore()));
                        int i = wyCostMakeMapper.insertWyCostMake(costMake);
                        result += i;
                        WyTb wyTb = wyTbMapper.selectById(2);
                        if(wyTb!=null&&costMake.getMakeTotal()!=null){
                            wyTb.setTbNeedMoney(wyTb.getTbNeedMoney().add(costMake.getMakeTotal()));
                            wyTbMapper.updateById(wyTb);
                        }
                    }

                } else if (methodId == 2) {/* 使用面积*单价/月 */
                    WyHomeVo wyHomeVo = wyBalconyunithomeVoMapper.selectWyHomeVoById(item.getBalconyunithomeId());
                    if (wyHomeVo.getUsableArea() != null){
                        costMake.setMakeTotal((new BigDecimal(wyHomeVo.getUsableArea().toString()).multiply(wyCost.getCostPrice()).multiply(BigDecimal.valueOf(30))).add(wyCost.getCostMore()));
                        int i = wyCostMakeMapper.insertWyCostMake(costMake);
                        result += i;
                        WyTb wyTb = wyTbMapper.selectById(2);
                        if(wyTb!=null&&costMake.getMakeTotal()!=null){
                            wyTb.setTbNeedMoney(wyTb.getTbNeedMoney().add(costMake.getMakeTotal()));
                            wyTbMapper.updateById(wyTb);
                        }
                    }

                } else if (methodId == 3) {/* 使用面积*单价/天 */
                    WyHomeVo wyHomeVo = wyBalconyunithomeVoMapper.selectWyHomeVoById(item.getBalconyunithomeId());
                    if (wyHomeVo.getUsableArea() != null){
                        costMake.setMakeTotal((new BigDecimal(wyHomeVo.getUsableArea().toString()).multiply(wyCost.getCostPrice()).multiply(BigDecimal.valueOf(daysBetween))).add(wyCost.getCostMore()));
                        int i = wyCostMakeMapper.insertWyCostMake(costMake);
                        result += i;
                        WyTb wyTb = wyTbMapper.selectById(2);
                        if(wyTb!=null&&costMake.getMakeTotal()!=null){
                            wyTb.setTbNeedMoney(wyTb.getTbNeedMoney().add(costMake.getMakeTotal()));
                            wyTbMapper.updateById(wyTb);
                        }
                    }
                } else if (methodId == 4) {/* 仪表走量*单价 */
                    Map map = new HashMap();
                    map.put("costId", wyCost.getCostId());
                    map.put("id", item.getBalconyunithomeId());
                    map.put("startTime", startTime);
                    map.put("endTime", endTime);
                    BigDecimal total = BigDecimal.ZERO;
                    List<WyYibiaoVo> wyYibiaoVos = wyBalconyunithomeVoMapper.selectWyYibiaoVoByMap(map);
                    if(!wyYibiaoVos.isEmpty()){
                        for (WyYibiaoVo wyYibiaoVo : wyYibiaoVos){
                            BigDecimal electricity = wyYibiaoVo.getYbMoyEndElectricity().subtract(wyYibiaoVo.getYbMoyBeginElectricity());
                            costMake.setMakeTotal(electricity.multiply(wyCost.getCostPrice()).add(wyCost.getCostMore()));
                            total=total.add(electricity.multiply(wyCost.getCostPrice()).add(wyCost.getCostMore()));
                        }
                        costMake.setMakeTotal(total);
                        int i = wyCostMakeMapper.insertWyCostMake(costMake);
                        result += i;
                        WyTb wyTb = wyTbMapper.selectById(3);
                        if(wyTb!=null&&costMake.getMakeTotal()!=null){
                            wyTb.setTbNeedMoney(wyTb.getTbNeedMoney().add(costMake.getMakeTotal()));
                            wyTbMapper.updateById(wyTb);
                        }
                    }
                } else if (methodId == 5) { /* 车位费*单价/天 */
                    Map map = new HashMap();
                    map.put("costId", wyCost.getCostId());
                    map.put("id", item.getBalconyunithomeId());
                    List<WyParkingRentVo> rentVos = wyBalconyunithomeVoMapper.selectWyParkingRentVoList(map);
                    BigDecimal total = BigDecimal.ZERO;
                    if (!rentVos.isEmpty()){
                        for (WyParkingRentVo rentVo : rentVos) {
                            if (rentVo.getEndTime().before(startTime) || rentVo.getStartTime().after(endTime)) {
                            } else if (rentVo.getStartTime().before(startTime) && rentVo.getEndTime().after(endTime)) {
                                total = total.add(BigDecimal.valueOf(daysBetween).multiply(wyCost.getCostPrice()).add(wyCost.getCostMore()));
                            } else if (rentVo.getStartTime().before(startTime) && rentVo.getEndTime().before(endTime) && rentVo.getEndTime().after(startTime)) {
                                total = total.add(BigDecimal.valueOf(getDaysBetween(startTime, rentVo.getEndTime())).multiply(wyCost.getCostPrice()).add(wyCost.getCostMore()));
                            } else if (rentVo.getStartTime().after(startTime) && rentVo.getStartTime().before(endTime) && rentVo.getEndTime().after(endTime)) {
                                total = total.add(BigDecimal.valueOf(getDaysBetween(rentVo.getStartTime(), endTime)).multiply(wyCost.getCostPrice()).add(wyCost.getCostMore()));
                            } else if (rentVo.getStartTime().after(startTime) && rentVo.getEndTime().before(endTime)) {
                                total = total.add(BigDecimal.valueOf(getDaysBetween(rentVo.getStartTime(), rentVo.getEndTime())).multiply(wyCost.getCostPrice()).add(wyCost.getCostMore()));
                            }
                        }
                        if (total==BigDecimal.ZERO){
                            continue;
                        }
                        costMake.setMakeTotal(total);
                        int i = wyCostMakeMapper.insertWyCostMake(costMake);
                        result += i;
                        WyTb wyTb = wyTbMapper.selectById(1);
                        if(wyTb!=null&&costMake.getMakeTotal()!=null){
                            wyTb.setTbNeedMoney(wyTb.getTbNeedMoney().add(costMake.getMakeTotal()));
                            wyTbMapper.updateById(wyTb);
                        }
                    }

                } else if (methodId == 6) { /* 车位费*单价/月 */
                    Map map = new HashMap();
                    map.put("costId", wyCost.getCostId());
                    map.put("id", item.getBalconyunithomeId());
                    List<WyParkingRentVo> rentVos = wyBalconyunithomeVoMapper.selectWyParkingRentVoList(map);
                    BigDecimal total = BigDecimal.ZERO;
                    if (!rentVos.isEmpty()){
                        for (WyParkingRentVo rentVo : rentVos) {
                            if (rentVo.getEndTime().before(startTime) || rentVo.getStartTime().after(endTime)) {
                            } else {
                                total = total.add(BigDecimal.valueOf(30).multiply(wyCost.getCostPrice()).add(wyCost.getCostMore()));
                            }
                        }
                        costMake.setMakeTotal(total);
                        int i = wyCostMakeMapper.insertWyCostMake(costMake);
                        result += i;
                        WyTb wyTb = wyTbMapper.selectById(1);
                        if(wyTb!=null&&costMake.getMakeTotal()!=null){
                            wyTb.setTbNeedMoney(wyTb.getTbNeedMoney().add(costMake.getMakeTotal()));
                            wyTbMapper.updateById(wyTb);
                        }
                    }
                }
            }
        }
        return result;
    }

    /**
     * 修改费用生成
     *
     * @param wyCostMake 费用生成
     * @return 结果
     */
    @Override
    public int updateWyCostMake(WyCostMake wyCostMake) {
        return wyCostMakeMapper.updateWyCostMake(wyCostMake);
    }

    /**
     * 批量删除费用生成
     *
     * @param makeIds 需要删除的费用生成主键
     * @return 结果
     */
    @Override
    public int deleteWyCostMakeByMakeIds(Long[] makeIds) {
        return wyCostMakeMapper.deleteWyCostMakeByMakeIds(makeIds);
    }

    /**
     * 删除费用生成信息
     *
     * @param makeId 费用生成主键
     * @return 结果
     */
    @Override
    public int deleteWyCostMakeByMakeId(Long makeId) {
        return wyCostMakeMapper.deleteWyCostMakeByMakeId(makeId);
    }

    private long getDaysBetween(Date startDate, Date endDate) {
        LocalDate localStartDate = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate localEndDate = endDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        return ChronoUnit.DAYS.between(localStartDate, localEndDate);
    }
}
